import { v2, Vec2 } from "cc";


export const mapNameConfig = [
    "HILLS", "DESERT", "FOREST", "GOBI", "MINE", "PLAINS", "SNOWY", "VOLCANO"
]

export class ModelGame {
    private static instance: ModelGame | null = null; // 保存单例实例

    public static getInstance(): ModelGame {
        if (this.instance === null) {
            this.instance = new ModelGame(); // 创建新的实例
        }
        return this.instance; // 返回已存在的实例
    }
    public nowInputCoin: number = 0;//当前投入资金数量
    public addInputCoin: number = 0;//危险之地加注的资金数量

    public getRandomPointInPolygon(polygon): Vec2 {
        const boundingBox = this.getBoundingBox(polygon);

        let randomPoint: Vec2;
        do {
            const randomX = Math.random() * (boundingBox.xMax - boundingBox.xMin) + boundingBox.xMin;
            const randomY = Math.random() * (boundingBox.yMax - boundingBox.yMin) + boundingBox.yMin;
            randomPoint = v2(randomX, randomY);
        } while (!this.isPointInPolygon(randomPoint, polygon));

        return randomPoint;
    }
    private getBoundingBox(polygon) {
        let xMin = Infinity;
        let xMax = -Infinity;
        let yMin = Infinity;
        let yMax = -Infinity;

        for (const vertex of polygon) {
            xMin = Math.min(xMin, vertex.x);
            xMax = Math.max(xMax, vertex.x);
            yMin = Math.min(yMin, vertex.y);
            yMax = Math.max(yMax, vertex.y);
        }

        return { xMin, xMax, yMin, yMax };
    }
    private isPointInPolygon(point, polygon) {
        let inside = false;
        const numVertices = polygon.length;

        for (let i = 0, j = numVertices - 1; i < numVertices; j = i++) {
            const vertex1 = polygon[i];
            const vertex2 = polygon[j];

            const intersect = ((vertex1.y > point.y) !== (vertex2.y > point.y)) &&
                (point.x < (vertex2.x - vertex1.x) * (point.y - vertex1.y) / (vertex2.y - vertex1.y) + vertex1.x);
            if (intersect) inside = !inside;
        }

        return inside;
    }
}